package main

import (
	"apedge/pkg/configs"
	"apedge/pkg/database"
	"apedge/pkg/loggers"
	"apedge/pkg/validators"
	"apedge/remote"
	"apedge/router"
	"apedge/task"
	"flag"
	"fmt"
	"os"
	"os/signal"
	"syscall"
)

var logger = loggers.LogInstance()

var (
	configFile = flag.String("config", "/etc/apedge/conf/config.yaml", "input config file like ../conf/dev/") // /data/apedge/conf/config.yaml  /etc/apedge/conf/config.yaml
)

func main() {
	logger.Infof("Apedge Backend Start! PID = %d", os.Getpid())
	flag.Parse()
	fmt.Println("config file path: ", *configFile)
	configs.InitConfig(*configFile, &configs.Config)
	loggers.InitLogger(configs.Config)
	database.InitDatabase(configs.Config)
	database.InitTables()
	validators.InitValidator()
	task.InitMqTask()
	logger.Infoln("version-----------------------------, 1")
	err := remote.InitEndpointPolicy()
	if err != nil {
		panic(err)
	}
	// start api server
	srv := router.StartApiServer(configs.Config)

	// quit when signal notifys
	quit := make(chan os.Signal, 1)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit

	router.StopApiServer(srv)
	logger.Infof("Apedge Exit!")
}
